package xw.qxypt.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class WeatherTranslateUtil {

	private static HashMap<String,String> pathTimeMap=new HashMap<String,String>();
	private static HashMap<String,String> distanceMap=new HashMap<String,String>();


	public static String getPathTimeMap(String userId) {
		return pathTimeMap.get(userId);
	}

	public static void setPathTimeMap(String userId,String time) {
		WeatherTranslateUtil.pathTimeMap.put(userId, time);
	}

	public static String getDistanceMap(String userId) {
		return WeatherTranslateUtil.pathTimeMap.get(userId);
	}

	public static void setDistanceMap(String userId,String dis) {
		WeatherTranslateUtil.distanceMap.put(userId, dis);
	}

	public static String[] getSkycon(String skycon) {
		String strArr[] = new String[2];
		strArr[0] = "雨";
		strArr[1] = "rain";
		if ("CLEAR_DAY".equals(skycon)) {
			strArr[0] = "晴";
			strArr[1] = "qingtian";
		}

		if ("CLEAR_NIGHT".equals(skycon)) {
			strArr[0] = "晴";
			strArr[1] = "qingye";
		}
		if ("PARTLY_CLOUDY_DAY".equals(skycon)) {
			strArr[0] = "多云";
			strArr[1] = "duoyun";
		}
		if ("PARTLY_CLOUDY_NIGHT".equals(skycon)) {
			strArr[0] = "多云";
			strArr[1] = "duoyun";
		}

		if ("CLOUDY".equals(skycon)) {
			strArr[0] = "阴";
			strArr[1] = "yintian";
		}
		if ("FOG".equals(skycon)) {
			strArr[0] = "雾";
			strArr[1] = "wu";
		}
		if ("WIND".equals(skycon)) {
			strArr[0] = "大风";
			strArr[1] = "feng";
		}
		if ("SNOW".equals(skycon)) {
			strArr[0] = "雪";
			strArr[1] = "snow";
		}

		return strArr;
	}

	// 根据雨强判断 大中小雨
	public static String getRainTypeByIntensity(String str) {
		String rainType = "";
		if (null != str && !str.equals("")) {
			double yuqiang = Double.parseDouble(str);
			if (yuqiang <= 0.25 && yuqiang > 0.03) {
				rainType = "小雨";
			} else if (yuqiang <= 0.35 && yuqiang > 0.25) {
				rainType = "中雨";
			} else if (yuqiang > 0.35) {
				rainType = "大雨";
			}
		}
		return rainType;
	}

	public static String getWindDjByValueMS(double fsValue) {
		String fsDj = "";
		if (fsValue <= 0.2) {
			fsDj = "无风";
		} else if (0.3 <= fsValue && fsValue <= 1.6) {
			fsDj = "一级";
		} else if (1.6 < fsValue && fsValue <= 3.4) {
			fsDj = "二级";
		} else if (3.4 < fsValue && fsValue <= 5.5) {
			fsDj = "三级";
		} else if (5.5 < fsValue && fsValue <= 8.0) {
			fsDj = "四级";
		} else if (8.0 < fsValue && fsValue <= 10.8) {
			fsDj = "五级";
		} else if (10.8 < fsValue && fsValue <= 13.9) {
			fsDj = "六级";
		} else if (13.9 < fsValue && fsValue <= 17.2) {
			fsDj = "七级";
		} else if (17.2 < fsValue && fsValue <= 20.8) {
			fsDj = "八级";
		} else if (20.8 < fsValue && fsValue <= 24.5) {
			fsDj = "九级";
		} else if (24.5 < fsValue && fsValue <= 28.5) {
			fsDj = "十级";
		} else if (28.5 < fsValue && fsValue <= 32.6) {
			fsDj = "十一级";
		} else if (32.6 < fsValue) {
			fsDj = "台风";
		}
		return fsDj;
	}

	// 根据风力判断 等级 km/h
	public static String getWindDjByValue(double fsValue) {
		String fsDj = "";
		if (fsValue <= 1) {
			fsDj = "无风";
		} else if (1 < fsValue && fsValue <= 5) {
			fsDj = "一级";
		} else if (5 < fsValue && fsValue <= 11) {
			fsDj = "二级";
		} else if (11 < fsValue && fsValue <= 19) {
			fsDj = "三级";
		} else if (19 < fsValue && fsValue <= 28) {
			fsDj = "四级";
		} else if (28 < fsValue && fsValue <= 38) {
			fsDj = "五级";
		} else if (38 < fsValue && fsValue <= 49) {
			fsDj = "六级";
		} else if (49 < fsValue && fsValue <= 61) {
			fsDj = "七级";
		} else if (61 < fsValue && fsValue <= 74) {
			fsDj = "八级";
		} else if (74 < fsValue && fsValue <= 88) {
			fsDj = "九级";
		} else if (88 < fsValue && fsValue <= 102) {
			fsDj = "十级";
		} else if (102 < fsValue && fsValue <= 117) {
			fsDj = "十一级";
		} else if (117 < fsValue) {
			fsDj = "台风";
		}
		return fsDj;
	}

	// 根据角度判断 风向
	public static String getWindDirByNum(String dir) {
		String fx = "";
		if (null != dir && !"".equals(dir)) {
			double va = Double.parseDouble(dir);
			if ((va > 337.5 && va <= 360) || (va <= 22.5)) {
				fx = "北";
			}

			if (va > 22.5 && va <= 67.5) {
				fx = "东北";
			}

			if (va > 67.5 && va <= 112.5) {
				fx = "东";
			}
			if (va > 112.5 && va <= 157.5) {
				fx = "东南";
			}
			if (va > 157.5 && va <= 202.5) {
				fx = "南";
			}
			if (va > 202.5 && va <= 247.5) {
				fx = "西南";
			}
			if (va > 247.5 && va <= 292.5) {
				fx = "西";
			}
			if (va > 292.5 && va <= 337.5) {
				fx = "西北";
			}
		}

		return fx;
	}

	// 根据经纬度查询天气，并返回预警等级(未按行车时间段查询)
	public static int queryForecastAlarm(String result, String time) {
		int alarmLevel = 0;
		double hourNum = Double.parseDouble(time) / (60 * 60);
		int num = (int) Math.ceil(hourNum);
		JSONObject obj = JSONObject.fromObject(result);

		JSONObject result0 = obj.getJSONObject("result");
		JSONObject hourly = result0.getJSONObject("hourly");
		String hourlyStatus = hourly.get("status").toString();

		HashMap hm = new HashMap();
		if (hourlyStatus.equals("ok")) {
			JSONArray skycon = hourly.getJSONArray("skycon");// 天气状况
			JSONArray precip = hourly.getJSONArray("precipitation");// 雨强

			for (int i = 0; i < num; i++) {
				JSONObject skyObj = skycon.getJSONObject(i);
				if (null != skyObj) {
					String hourStr = skyObj.getString("datetime");
					String hourCon = skyObj.getString("value");
					String precipValue = precip.getJSONObject(i).getString(
							"value");
					if (hourCon.equals("CLEAR_DAY")
							|| hourCon.equals("CLEAR_NIGHT")
							|| hourCon.equals("PARTLY_CLOUDY_DAY")
							|| hourCon.equals("PARTLY_CLOUDY_NIGHT")
							|| hourCon.equals("CLOUDY")) {
						alarmLevel = 0;
					} else if (hourCon.equals("RAIN") || hourCon.equals("SNOW")
							|| hourCon.equals("FOG")) {
						if (alarmLevel < 1) {
							alarmLevel = 1;
						}

					}

				}

			}
		}

		// 逐分钟
		JSONObject minutely = result0.getJSONObject("minutely");
		String minStatus = minutely.getString("status");
		if (minStatus.equals("ok")) {
			// 获取雨强

			JSONArray preciArr = minutely.getJSONArray("precipitation");
			JSONArray preciTwoArr = minutely.getJSONArray("precipitation_2h");
			if (num >= 2) {// 当路程大于 2个h 时，查询 2h 逐分钟的 雨强
				for (int i = 0; i < preciTwoArr.size(); i++) {
					double preVal = preciTwoArr.getDouble(i);
					if (preVal >= 0.03) {
						if (alarmLevel < 1) {
							alarmLevel = 1;
						}
					}

				}
			}

			if (num >= 1) {// 当路程大于 1个h 时，查询1h 逐分钟的 雨强
				for (int i = 0; i < preciArr.size(); i++) {// preciArr.getString(i)
					double preVal = preciArr.getDouble(i);
					if (preVal >= 0.03) {
						if (alarmLevel < 1) {
							alarmLevel = 1;
						}
					}
				}

			}

		}

		return alarmLevel;
	}


	// 逆地理编码
//	public static String KEY = "8833d597f977aa2c6ec5565b44a36f93";//6dff5692ef12a02d4e94a1b8b12195c3
	public static String KEY = "8833d597f977aa2c6ec5565b44a36f93";
	private static Pattern pattern = Pattern
			.compile("\"location\":\"(\\d+\\.\\d+),(\\d+\\.\\d+)\"");

	public static String  gpsToAddress(double lng, double lat,String key) {
		String address="";
		InputStreamReader insr = null;
		BufferedReader br = null;
		try {
			URL myURL = null;
			URLConnection httpsConn = null;
			try {
				myURL = new URL("https://restapi.amap.com/v3/geocode/regeo?output=json&location="+lng+","+lat+"&key="+key);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			
			httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
			if (httpsConn != null) {
				insr = new InputStreamReader(httpsConn.getInputStream(),
						"UTF-8");
				br = new BufferedReader(insr);
				String data = "";
				String line = null;
				while ((line = br.readLine()) != null) {
					data += line;
				}
//				System.out.println("========================data="+data);
				JSONObject obj=JSONObject.fromObject(data);
				String status=obj.getString("status");
				String infocode=obj.getString("infocode");
				if(status.equals("1")){//成功
					JSONObject regObj=obj.getJSONObject("regeocode");
					String str=regObj.getString("formatted_address");
//					System.out.println("str="+str);
					address=str;
				}else if("10003".equals(infocode)){
					address="超出日访问量";
				}else if("10004".equals(infocode)){
					address="单位时间内访问过于频繁";
				}else if("10010".equals(infocode)){
					address="IP访问超限";
				}else if("10003".equals(infocode)){
					address="超出日访问量";
				}else if("20003".equals(infocode)){
					address="超出日访问量";
				}else if("10003".equals(infocode)){
					address="其他未知错误";
				}else if("10020".equals(infocode)){
					address="每分钟访问超时限制";
				}else{
					address=infocode;
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			return address;
		}finally{
			if(null!=br){
				try {
					br.close();
				} catch (IOException e) { 
					e.printStackTrace();
				}
			}
			if(null!=insr){
				try {
					insr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return address;
	}
   //地理编码：  批量查询： 根据地址查询经纬度
	public static String addressToGPSBatch(String address,String key) throws UnsupportedEncodingException{
	     String address1=URLEncoder.encode(address,"utf-8");
		 String locStr="";
		 InputStreamReader insr = null;
		 BufferedReader br = null;
		try {
			URL myURL = null;
			URLConnection httpsConn = null;
			try {
				String urlSr="https://restapi.amap.com/v3/geocode/geo?output=json&address="+address1+"&batch=true&key="+key;
				myURL = new URL(urlSr);
				httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
				if (httpsConn != null) {
					insr = new InputStreamReader(httpsConn.getInputStream(),
							"UTF-8");
					br = new BufferedReader(insr);
					String data = "";
					String line = null;
					while ((line = br.readLine()) != null) {
						data += line;
					}
					JSONObject obj=JSONObject.fromObject(data);
					String status=obj.getString("status");
					if(status.equals("1")){//成功
						JSONArray regArr=obj.getJSONArray("geocodes");
						for(int i=0;i<regArr.size();i++){
							 JSONObject regObj=regArr.getJSONObject(i);
							 String loc=regObj.getString("location");
							 locStr+=loc+";";
						}
						 if(locStr.endsWith(";")){
							 locStr=locStr.substring(0,locStr.length()-1);
						 }

					}else{
						locStr="请求失败";
					}
					return locStr;
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		 
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			if(null!=br){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null!=insr){
				try {
					insr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	 //地理编码： 根据地址查询经纬度
	public static double[] addressToGPS(String address,String key) throws UnsupportedEncodingException { 
		InputStreamReader insr = null;
		BufferedReader br = null;
		try {
			URL myURL = null;
			URLConnection httpsConn = null;
			String address1=URLEncoder.encode(address,"utf-8");
			try {
				myURL = new URL("https://restapi.amap.com/v3/geocode/geo?output=json&address="+address1+"&key="+key);
				httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
				if (httpsConn != null) {
					insr = new InputStreamReader(httpsConn.getInputStream(),
							"UTF-8");
					br = new BufferedReader(insr);
					String data = "";
					String line = null;
					while ((line = br.readLine()) != null) {
						data += line;
					}
					Matcher matcher = pattern.matcher(data);
					if (matcher.find() && matcher.groupCount() == 2) {
						double[] gps = new double[2];
						gps[0] = Double.valueOf(matcher.group(1));
						gps[1] = Double.valueOf(matcher.group(2));
						System.out.println("===========gps="+gps[0]+"=="+gps[1]+"=========");
						return gps;
					}
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			 
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			if(null!=br){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null!=insr){
				try {
					insr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	

	
	public static void main(String[] args) {

		
	}
}
